// This is wrong ... to put this file among controllers.Will be moved later

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerFactory;

class XMLFormatter{

	def xslt  =   new StringBuffer("" +
	               "<xsl:stylesheet " +
	               "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" " +
	               "xmlns:xalan=\"http://xml.apache.org/xslt\" version=\"1.0\">" +
	               "<xsl:output method=\"xml\" indent=\"yes\" xalan:indent-amount=\"3\"/> " +
	               "<xsl:template match=\"* | @*\">" +
	               "<xsl:copy><xsl:copy-of select=\"@*\"/>" +
	               "<xsl:apply-templates/></xsl:copy>" +
	               "</xsl:template>" +
               	       "</xsl:stylesheet>");
               	       
	def format = { str ->
	
	
		def factory = TransformerFactory.newInstance()
		//factory.setAttribute("indent-number", new Integer(2));		
		
		def stream = new ByteArrayInputStream(xslt.toString().getBytes("UTF-8"));		
		Transformer transformer = factory.newTransformer(new StreamSource(stream))
		
		//Transformer transformer = factory.newTransformer()
		
		//transformer.setOutputProperty(OutputKeys.INDENT, 'yes')
		//transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		//transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
		StreamResult result = new StreamResult(new StringWriter())
        	transformer.transform(new StreamSource(new ByteArrayInputStream(str.getBytes())), result)
		
		result.writer.toString()
	}
}